package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.server.BlackListService;
import com.tanhua.dubbo.server.UserInfoService;
import com.tanhua.model.domain.BlackList;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class BlackListApiImpl implements BlackListApi {
    @Autowired
    private BlackListService blackListService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public PageResult getBlackList(int page, int pagesize, Long userId) {
        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackList::getUserId, userId);
        Page<BlackList> blackListPage = new Page<>(page, pagesize);
        List<BlackList> blackLists = blackListService.page(blackListPage, queryWrapper).getRecords();

        List<UserInfo> userInfoList = blackLists.stream().map((item) -> {
            Long blackUserId = item.getBlackUserId();
//            LambdaQueryWrapper<UserInfo> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(UserInfo::getId, blackUserId);
            return userInfoService.getById(blackUserId);
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult();
        BeanUtils.copyProperties(blackListPage, pageResult);
        pageResult.setItems(userInfoList);
        return pageResult;
    }

    @Override
    public void deleteBlackList(Long userId, Long blackUserId) {
        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackList::getBlackUserId, blackUserId);
        queryWrapper.eq(BlackList::getUserId, userId);
        blackListService.remove(queryWrapper);
    }
}
